﻿package core.drawing
{
    import core.enum.*;
    import core.image.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    final public class DrawingTool extends Tool
    {
        var pt:Point;
        var pr:Point;
        var po:Point;
        var rect:Rectangle;
        var ts:Sprite;

        public function DrawingTool(param1:Tuyitu)
        {
            super(param1, ToolType.Drawing, false);
            return;
        }// end function

        override public function Reset() : void
        {
            type.attitude.locked = Boolean(ed.canvas.layer.type != LayerType.Bitmap);
            if (AppState.OnCanvas)
            {
                ed.cursor.Set(type);
            }
            ed.options.SetTool(type);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (!type.attitude.locked)
            {
                this.UpdateReferences();
                ed.cursor.freeze = true;
                ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
                ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
                history.Prepare(options.action);
                this.pt = canvas.GetMousePosition();
                this.pr = canvas.GetRealMousePosition();
                this.ts = new Sprite();
                this.rect = new Rectangle();
            }
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            canvas.coating.Clear();
            this.ts.graphics.clear();
            this.po = canvas.GetRealMousePosition();
            switch(options.action)
            {
                case Action.DrawLine:
                {
                    this.ts.graphics.lineStyle(options.linesize * canvas.zoomlevel, 11184810, 0.75);
                    this.ts.graphics.moveTo(this.pr.x, this.pr.y);
                    this.ts.graphics.lineTo(this.po.x, this.po.y);
                    break;
                }
                case Action.DrawEllipse:
                {
                    this.rect = Misc.CalculateRectangle(this.pr, this.po);
                    this.ts.graphics.beginFill(11184810, 0);
                    this.ts.graphics.lineStyle(1, 11184810, 0.75);
                    this.ts.graphics.drawEllipse(this.rect.x, this.rect.y, (this.rect.width - 1), (this.rect.height - 1));
                    this.ts.graphics.endFill();
                    break;
                }
                case Action.DrawRect:
                {
                    this.rect = Misc.CalculateRectangle(this.pr, this.po);
                    this.ts.graphics.beginFill(11184810, 0);
                    this.ts.graphics.lineStyle(1, 11184810, 0.75);
                    this.ts.graphics.drawRect(this.rect.x, this.rect.y, (this.rect.width - 1), (this.rect.height - 1));
                    this.ts.graphics.endFill();
                    break;
                }
                case Action.DrawRoundRect:
                {
                    this.rect = Misc.CalculateRectangle(this.pr, this.po);
                    this.ts.graphics.beginFill(11184810, 0);
                    this.ts.graphics.lineStyle(1, 11184810, 0.75);
                    this.ts.graphics.drawRoundRect(this.rect.x, this.rect.y, (this.rect.width - 1), (this.rect.height - 1), options.roundrectradie * canvas.zoomlevel);
                    this.ts.graphics.endFill();
                    break;
                }
                default:
                {
                    break;
                }
            }
            canvas.coating.bitmapData.draw(this.ts);
            canvas.coating.Redraw();
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            var _loc_2:uint = 0;
            var _loc_3:Point = null;
            var _loc_4:Sprite = null;
            var _loc_5:BitmapData = null;
            var _loc_6:BitmapData = null;
            var _loc_7:BitmapData = null;
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            canvas.coating.Clear();
            canvas.coating.Redraw();
            this.ts.graphics.clear();
            this.po = canvas.GetMousePosition();
            if (this.pt.x != this.po.x || this.pt.y != this.po.y)
            {
                this.rect = new Rectangle();
                this.ts.alpha = options.opacity;
                this.ts.blendMode = options.blendmode;
                _loc_2 = ed.toolbar.GetColor(2);
                if (_loc_2 == 4294967295)
                {
                    _loc_2 = 4294967294;
                }
                _loc_3 = layer.offset;
                switch(options.action)
                {
                    case Action.DrawLine:
                    {
                        this.ts.graphics.lineStyle(options.linesize, ed.toolbar.GetColor(1));
                        this.ts.graphics.moveTo(this.pt.x, this.pt.y);
                        this.ts.graphics.lineTo(this.po.x, this.po.y);
                        break;
                    }
                    case Action.DrawEllipse:
                    {
                        this.rect = Misc.CalculateRectangle(this.pt, this.po);
                        this.ts.graphics.beginFill(_loc_2, options.ellipsefill ? (1) : (0));
                        if (options.ellipseborder > 0)
                        {
                            this.ts.graphics.lineStyle(options.ellipseborder, ed.toolbar.GetColor(1), 1, true);
                        }
                        this.ts.graphics.drawEllipse(this.rect.x, this.rect.y, (this.rect.width - 1), (this.rect.height - 1));
                        this.ts.graphics.endFill();
                        break;
                    }
                    case Action.DrawRect:
                    {
                        this.rect = Misc.CalculateRectangle(this.pt, this.po);
                        this.ts.graphics.beginFill(_loc_2, options.rectfill ? (1) : (0));
                        if (options.rectborder > 0)
                        {
                            this.ts.graphics.lineStyle(options.rectborder, ed.toolbar.GetColor(1), 1, true);
                        }
                        this.ts.graphics.drawRect(this.rect.x, this.rect.y, (this.rect.width - 1), (this.rect.height - 1));
                        this.ts.graphics.endFill();
                        break;
                    }
                    case Action.DrawRoundRect:
                    {
                        this.rect = Misc.CalculateRectangle(this.pt, this.po);
                        this.ts.graphics.beginFill(_loc_2, options.roundrectfill ? (1) : (0));
                        if (options.roundrectborder > 0)
                        {
                            this.ts.graphics.lineStyle(options.roundrectborder, ed.toolbar.GetColor(1), 1, true);
                        }
                        this.ts.graphics.drawRoundRect(this.rect.x, this.rect.y, (this.rect.width - 1), (this.rect.height - 1), options.roundrectradie);
                        this.ts.graphics.endFill();
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                _loc_4 = new Sprite();
                _loc_4.addChild(this.ts);
                _loc_5 = layer.SetBitmapEditMode(1, new Rectangle(0, 0, canvas.dw, canvas.dh));
                if (this.ts.blendMode != "normal" && layer.bitmap.bitmapData != null)
                {
                    _loc_5.copyPixels(layer.bitmap.bitmapData, layer.bitmap.bitmapData.rect, new Point(layer.x, layer.y));
                }
                _loc_5.draw(_loc_4);
                if (selection != null)
                {
                    _loc_6 = _loc_5.clone();
                    _loc_7 = selection.AlphaMask();
                    _loc_5.fillRect(_loc_5.rect, 16777215);
                    _loc_5.copyPixels(_loc_6, _loc_6.rect, _loc_6.rect.topLeft, _loc_7, _loc_7.rect.topLeft, true);
                }
                this.ts = null;
            }
            layer.RemoveBitmapEditMode();
            canvas.UpdateAfterEdit();
            history.Commit();
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

    }
}
